package cn.mw.cmdb.service.impl;

import cn.mw.cmdb.entity.ModelGroupInfo;
import cn.mw.cmdb.entity.TableSearchConditionDto;
import cn.mw.cmdb.mongoMapper.TableSearchConditionMapper;
import cn.mw.cmdb.util.CMDBTool;
import cn.mw.components.mongodb.tool.CriteriaWrapper;
import cn.mw.microMonitorCommon.security.dto.MwUser;
import cn.mw.microMonitorCommon.security.utils.SecurityUtils;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

public class TableSearchConditionServiceImpl extends ModelBaseServiceImpl<TableSearchConditionDto, Object> {

    public static final String ID = "tableSearchConditionService";

    private TableSearchConditionMapper tableSearchConditionMapper;

    public TableSearchConditionServiceImpl(TableSearchConditionMapper tableSearchConditionMapper) {
        this.tableSearchConditionMapper = tableSearchConditionMapper;
    }

    @Override
    public TableSearchConditionDto genObject() {
        return new TableSearchConditionDto();
    }

    @Override
    Object doAdd(TableSearchConditionDto param) throws Exception {
        MwUser user = SecurityUtils.getUser();
        if (user != null) {
            Long userId = user.getId();
            param.setCreator(userId);
        }
        return tableSearchConditionMapper.insert(param);
    }

    @Override
    boolean doUpdate(TableSearchConditionDto param) throws Exception {
        Query query = new Query(CriteriaWrapper.where(ModelGroupInfo::getId).is(param.getId()));
        Update update = CMDBTool.genMongoUpdate(param, null, null);
        CMDBTool.updateFirst(tableSearchConditionMapper, query, update);
        return true;
    }

    @Override
    boolean doRemove(TableSearchConditionDto param) throws Exception {
        CMDBTool.remove(tableSearchConditionMapper, param.getId(), TableSearchConditionDto.class, false);
        return true;
    }

    @Override
    Object doBatchAdd(Object params) {
        return false;
    }

    @Override
    boolean doBatchUpdate(Object params) {
        return false;
    }

    @Override
    public TableSearchConditionDto doSelectById(Object id) {
        TableSearchConditionDto tableSearchConditionDto= (TableSearchConditionDto) id;
        return tableSearchConditionMapper.findById(tableSearchConditionDto.getId());
    }

    @Override
    public List<TableSearchConditionDto> doSelectList(Object param) throws Exception {
        //获取参数值对对应属性类型,并生成查询条件
        MwUser user = SecurityUtils.getUser();
        Long userId = user.getId();
        Query query = new Query(CriteriaWrapper.where(TableSearchConditionDto::getCreator).is(userId));
        query.fields().include(CMDBTool.getFieldName(TableSearchConditionDto::getId),CMDBTool.getFieldName(TableSearchConditionDto::getName));
        List<TableSearchConditionDto> ret = CMDBTool.selectList(TableSearchConditionDto.class, tableSearchConditionMapper, query, null);
        return ret;
    }

    @Override
    boolean doBatchRemove(Object params) throws Exception {
        return false;
    }
}
